/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.newworld.dmsapp2.activity.serviceapply;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;

import com.newworld.dmsapp2.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ViewPagerActivity extends Activity {

	private ViewPager mViewPager;
	static final String LOG_TAG = "ViewPagerActivity";
	private Bundle b;

	private Intent intent;
	private String imageString1 = "";
	private String imageString2 = "";
	private String[] strings;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mViewPager = new HackyViewPager(this);
		setContentView(mViewPager);
		intent = getIntent();
		b = intent.getExtras();
		if (null != b.getString("image2") && !b.getString("image2").equals("")) {
			strings = new String[2];
			imageString1 = b.getString("image1");
			imageString2 = b.getString("image2");
			strings[0] = imageString1;
			strings[1] = imageString2;
		} else {
			strings = new String[1];
			imageString1 = b.getString("image1");
			strings[0] = imageString1;
		}

		mViewPager.setAdapter(new SamplePagerAdapter());
	}

	class SamplePagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return strings.length;
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {

			PhotoView photoView = new PhotoView(container.getContext());

			// 显示图片的配置
			DisplayImageOptions options = new DisplayImageOptions.Builder()
					.cacheInMemory(false).cacheOnDisk(false)
					.bitmapConfig(Bitmap.Config.RGB_565).build();

			String imageUrl = strings[position];
			ImageLoader.getInstance()
					.displayImage(imageUrl, photoView, options);

			// Now just add PhotoView to ViewPager and return it
			container.addView(photoView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);

			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
			ImageLoader.getInstance().cancelDisplayTask((ImageView) object);
			Log.e(LOG_TAG, "destroyItem");
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return false;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

}
